package com.example.jnuexperiment2025;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Hello52412Activity extends AppCompatActivity {

    // 定义String类型的成员变量clickedNumber用于保存用户点击的数字
    private String clickedNumber = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_hello52412);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始化所有按钮的点击事件
        initButtonClickListeners();
    }

    private void initButtonClickListeners() {
        // 为10个数字按钮添加Click事件
        findViewById(R.id.buttonview_number_0).setOnClickListener(v -> appendNumber("0"));
        findViewById(R.id.buttonview_number_1).setOnClickListener(v -> appendNumber("1"));
        findViewById(R.id.buttonview_number_2).setOnClickListener(v -> appendNumber("2"));
        findViewById(R.id.buttonview_number_3).setOnClickListener(v -> appendNumber("3"));
        findViewById(R.id.buttonview_number_4).setOnClickListener(v -> appendNumber("4"));
        findViewById(R.id.buttonview_number_5).setOnClickListener(v -> appendNumber("5"));
        findViewById(R.id.buttonview_number_6).setOnClickListener(v -> appendNumber("6"));
        findViewById(R.id.buttonview_number_7).setOnClickListener(v -> appendNumber("7"));
        findViewById(R.id.buttonview_number_8).setOnClickListener(v -> appendNumber("8"));
        findViewById(R.id.buttonview_number_9).setOnClickListener(v -> appendNumber("9"));

        // Remove按钮点击事件
        findViewById(R.id.buttonview_remove).setOnClickListener(v -> showClearConfirmDialog());

        // Input按钮点击事件
        findViewById(R.id.buttonview_input).setOnClickListener(v -> showInputToast());
    }

    // 将数字添加到clickedNumber中
    private void appendNumber(String number) {
        clickedNumber += number;
    }

    // 显示清空确认对话框
    private void showClearConfirmDialog() {
        new AlertDialog.Builder(this)
                .setMessage(getString(R.string.dialog_confirm_clear))
                .setPositiveButton(getString(R.string.dialog_yes), (dialog, which) -> {
                    // 用户选择确定，清空clickedNumber
                    clickedNumber = "";
                })
                .setNegativeButton(getString(R.string.dialog_no), (dialog, which) -> {
                    // 用户选择取消，不处理
                })
                .show();
    }

    // 显示输入的数字Toast
    private void showInputToast() {
        String message = getString(R.string.toast_input_message, clickedNumber);
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}